<template>
  <div class="app-container">
    <!-- 基础信息 -->
    <h5 class="titleInfo">权限编辑</h5>
    <el-form class="form" ref="form"  :rules="rules" label-width="130px" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="权限类型" prop="type">
            <el-select placeholder="请选择" v-model="form.type">
              <el-option
                v-for="item in typeOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="权限字段" prop="code">
            <el-input v-model="form.code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="form.description"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="所属系统">
        <Fromplatfrom @childfun="childfun" ref="subSysMsg" values="disable"></Fromplatfrom>
      </el-form-item>
      <el-row>
        <el-col :span="24">
          <el-form-item label="权限状态" prop="status">
            <el-select placeholder="请选择" v-model="form.status">
              <el-option
                v-for="item in statusOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button style="margin-top:30px" type="primary" @click="submitFn('form')">立即提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {
  updateAdminPermission,
  getAdminPermissionById
} from "@/api/permissionsManage";
import Fromplatfrom from "../components/platform";
export default {
  components: { Fromplatfrom },
  name: "permissionsManageEdit",
  data() {
    return {
      form: {},
      typeOption: [
        {
          value: 0,
          label: "菜单"
        }
        // {
        //     value: 1,
        //     label: '按钮'
        // }
      ],
      statusOption: [
        {
          value: 0,
          label: "正常"
        },
        {
          value: 1,
          label: "关闭"
        }
      ],
      rules: {
        name: [{ required: true, message: "请输入权限名称", trigger: "blur" }],
        type: [{ required: true, message: "请选择权限类型", trigger: "blur" }],
        code: [{ required: true, message: "请输入权限字段", trigger: "blur" }],
        description: [
          { required: true, message: "请输入权限描述", trigger: "blur" }
        ],
        status: [{ required: true, message: "请选择权限状态", trigger: "blur" }]
      },
      userId: null
    };
  },
  inject: ["reload"],
  created() {
    this.userId = Number(this.$route.query.userId);
    this.getInfo();
  },
  methods: {
    childfun(val) {
      console.log(val);
      this.form.subSys = val === "" ? -1 : val;
    },
    getInfo() {
      let datas = { id: this.userId };
      getAdminPermissionById(datas)
        .then(res => {
          if (!res.data.code) {
            let resData = res.data.data;
            this.$refs.subSysMsg.getMessage(res.data.data.subSys);
            this.form = {
              id: this.userId,
              name: resData.name,
              type: resData.type,
              code: resData.code,
              description: resData.description,
              status: resData.status
            };
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    submitFn(e) {
      this.$refs[e].validate(valid => {
        if (valid) {
          let datas = this.form;
          console.log(datas);
          updateAdminPermission(datas)
            .then(res => {
              console.log(res);
              if (!res.data.code) {
                this.$message({
                  showClose: true,
                  message: "编辑成功",
                  type: "success"
                });
                this.$router.push("/permissionsManageIndex");
                this.reload();
              } else {
                this.$message({
                  showClose: true,
                  message: res.data.msg,
                  type: "warning"
                });
              }
            })
            .catch(error => {
              this.$message.error(error);
            });
        } else {
          this.$message({
            message: "必填项不能为空",
            type: "warning"
          });
          return false;
        }
      });
    }
  }
};
</script>
<style scoped>
.app-container {
  padding: 0.1px !important;
}
</style>
